package com.dao.impl;

import com.dao.RedisDao;
import com.dao.RedisGeneratorDao;
import com.entity.Orderinfo;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


@Repository(value = "chartRedisDao")
public class RedisDaoImpl extends RedisGeneratorDao implements RedisDao {

    /**
     * 根据订单号  查询 redis中的订单对象（带上了订单详情）
     * @param keyStr
     * @return
     */
    //    @Override
    public Object getObj(String keyStr) {
        //执行redisTemplate模板类 完成功能，需要定义RedisCallback接口的实现类，使用匿名实现类就直接new接口，在括号中实现 需要实现的方法即可
        Orderinfo orderinfo = redisTemplate.execute(new RedisCallback<Orderinfo>() {
            public  Orderinfo doInRedis(RedisConnection connection)
                    throws DataAccessException {
                GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();

                RedisSerializer<String> serializer = getRedisSerializer();//得到当前redis的序列化器，通过序列化器传入key得到value
                byte[] key = serializer.serialize(keyStr);//redis提供正确的key就可以查到对应的value，得正确序列化
                byte[] value = connection.get(key);
                //默认的序列化器反序列化
                //return (List<Chart>)new JdkSerializationRedisSerializer().deserialize(value);
                return (Orderinfo)jackson2JsonRedisSerializer.deserialize(value);
            }
        },true,false);
        return orderinfo;
    }

    /**
     * 将订单序列化后存放的redis  key就是订单的id
     * @param obj
     * @param keyStr
     * @return
     */
    public boolean pushObj(Object obj,String keyStr) {
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();

                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key = serializer.serialize(keyStr);
                //使用默认的jdk序列化器 存放在redis无法解读，但不影响反序列化
                //byte[] name = new JdkSerializationRedisSerializer().serialize(charts);
                byte[] name = jackson2JsonRedisSerializer.serialize(obj);
                //connection.set(key, name);
                connection.setEx(key,30,name);//设置30秒过期
                System.out.println(key+" "+name);
                return true;
            }
        },true,false);
        return result;
    }


    public Set<Serializable> getKeys(String keyStr){
        return redisTemplate.keys(keyStr);
    }


    public void delete(String keyStr){
        redisTemplate.delete(keyStr);
    }

    //存取集合对象示例
//    @Override
//    public List<Chart> getCharts(String typeName) {
//        List<Chart> charts = redisTemplate.execute(new RedisCallback<List<Chart>>() {
//            public  List<Chart> doInRedis(RedisConnection connection)
//                    throws DataAccessException {
//                GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
//
//                RedisSerializer<String> serializer = getRedisSerializer();
//                byte[] key = serializer.serialize(typeName);
//                byte[] value = connection.get(key);
//                //默认的序列化器反序列化
//                //return (List<Chart>)new JdkSerializationRedisSerializer().deserialize(value);
//                return (List<Chart>)jackson2JsonRedisSerializer.deserialize(value);
//            }
//        },true,false);
//        return charts;
//    }
//
//    public boolean pushCharts(List<Chart> charts,String typeName) {
//        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
//            public Boolean doInRedis(RedisConnection connection)
//                    throws DataAccessException {
//                GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
//
//                RedisSerializer<String> serializer = getRedisSerializer();
//                byte[] key = serializer.serialize(typeName);
//                //使用默认的jdk序列化器 存放在redis无法解读，但不影响反序列化
//                //byte[] name = new JdkSerializationRedisSerializer().serialize(charts);
//                byte[] name = jackson2JsonRedisSerializer.serialize(charts);
//                connection.set(key, name);
//                System.out.println(key+" "+name);
//                return true;
//            }
//        },true,false);
//        return result;
//    }
}